import json
import time
from django.http import JsonResponse
from web.ais.TengXunYuanBao.JiChuBan import YuanBaoJiChuBan
from django.http import StreamingHttpResponse
tengxunyuanbao = YuanBaoJiChuBan()


# 获取模型
def get_model(request):
    models = [
        {
            "name": "腾讯元宝",
            "model": [
                {
                    "name": "hunyuan(基础)",
                    "value": "hunyuan_gpt_175B_0404"
                },
                {
                    "name": "hunyuanT1(深度思考)",
                    "value": "hunyuan_t1"
                },
                {
                    "name": "deep_seek_v3(基础)",
                    "value": "deep_seek_v3"
                },
                {
                    "name": "deep_seek(深度思考)",
                    "value": "deep_seek"
                }
            ],
            "current_model": "deep_seek_v3",
            "platform": "TengXunYuanBao"
        }
    ]
    return JsonResponse(models, safe=False)

# 创建新对话
def create_new_chat(request):
    chat_id = tengxunyuanbao.create_chat()
    return JsonResponse(chat_id)


# 对话函数
def dialogue(message, chat_id, model, multimedia):
    response = tengxunyuanbao.send_message(message, chat_id, model, multimedia=multimedia)
    for line in response.iter_lines():
                if line:
                    try:
                        text = line.decode('utf-8')
                        if text.startswith('data: '):
                            text = text.replace('data: ', '')
                            if text == "[DONE]":
                                yield 'data: {"type": "end", "msg": "对话结束"}\n\n'
                                break
                            text = json.loads(text)
                            if text['type'] == "text":
                                # 回复
                                if text.get("msg") is not None:
                                    yield f'data: {json.dumps(text, ensure_ascii=False)}\n\n'
                                    time.sleep(0.1)
                                else:
                                    continue
                            elif text['type'] == "think":
                                yield f'data: {json.dumps(text, ensure_ascii=False)}\n\n'
                                time.sleep(0.1)
                            else:
                                continue
                               

                    except json.JSONDecodeError as e:
                        pass
                    except KeyError as e:
                        pass

# 发送消息
def send_message(request):
    message = request.GET.get('message')
    chat_id = request.GET.get('chat_id')
    model = request.GET.get('model')
    multimedia = request.GET.get('multimedia')
    if multimedia is None or multimedia == '':
        multimedia = []
    else:
        multimedia = json.loads(multimedia)
    response = StreamingHttpResponse(dialogue(message, chat_id, model, multimedia), content_type='text/event-stream')
    response.headers['Cache-Control'] = 'no-cache'
    return response


